import * as PIXI from "pixi.js";
export default class EnemySprite {
  constructor() {
    this.loadTextures();
    this.container = new PIXI.AnimatedSprite(this.runTexture);
    this.container.animationSpeed = 0.1;
    this.container.position.set(
      this.getRandomInt(50, window.innerWidth - 100),
      this.getRandomInt(350, window.innerHeight - 230)
    );
    this.container.play();
    this.hp = 5;
    this.runNum = 0;
    this.attackNum = 0;
  }
  getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值，含最小值
  }
  attack() {
    this.container.textures = this.attackTexture;
    this.container.animationSpeed = 0.1;
    this.container.play();
  }
  run() {
    this.container.textures = this.runTexture;
    this.container.animationSpeed = 0.1;
    this.container.play();
  }
  dead() {
    this.container.y += 34;
    this.container.textures = this.deadTexture;
    this.container.animationSpeed = 0.05;
    this.container.loop = false;
    this.container.play();
  }
  hurt() {
    this.hp -= 2;
    if (this.hp <= 0) {
      this.dead();
    }
  }
  loadTextures() {
    const baseIdleTexture = PIXI.BaseTexture.from(
      "/public/enemy/Orc_Berserk/Idle.png"
    );
    this.idleTexture = [];
    this.idleTexture.push(
      new PIXI.Texture(baseIdleTexture, new PIXI.Rectangle(17, 32, 67, 64))
    );
    this.idleTexture.push(
      new PIXI.Texture(baseIdleTexture, new PIXI.Rectangle(111, 32, 67, 64))
    );
    this.idleTexture.push(
      new PIXI.Texture(baseIdleTexture, new PIXI.Rectangle(209, 32, 67, 64))
    );
    this.idleTexture.push(
      new PIXI.Texture(baseIdleTexture, new PIXI.Rectangle(305, 32, 67, 64))
    );
    this.idleTexture.push(
      new PIXI.Texture(baseIdleTexture, new PIXI.Rectangle(401, 32, 67, 64))
    );

    const baseRunTexture = PIXI.BaseTexture.from(
      "/public/enemy/Orc_Berserk/Run.png"
    );
    this.runTexture = [];
    this.runTexture.push(
      new PIXI.Texture(baseRunTexture, new PIXI.Rectangle(23, 32, 49, 64))
    );
    this.runTexture.push(
      new PIXI.Texture(baseRunTexture, new PIXI.Rectangle(120, 32, 67, 64))
    );
    this.runTexture.push(
      new PIXI.Texture(baseRunTexture, new PIXI.Rectangle(215, 32, 67, 64))
    );
    this.runTexture.push(
      new PIXI.Texture(baseRunTexture, new PIXI.Rectangle(312, 32, 67, 64))
    );
    this.runTexture.push(
      new PIXI.Texture(baseRunTexture, new PIXI.Rectangle(408, 32, 67, 64))
    );
    this.runTexture.push(
      new PIXI.Texture(baseRunTexture, new PIXI.Rectangle(503, 32, 67, 64))
    );

    const baseAttackTexture = PIXI.BaseTexture.from(
      "/public/enemy/Orc_Berserk/Attack_1.png"
    );
    this.attackTexture = [];
    this.attackTexture.push(
      new PIXI.Texture(baseAttackTexture, new PIXI.Rectangle(8, 33, 85, 63))
    );
    this.attackTexture.push(
      new PIXI.Texture(baseAttackTexture, new PIXI.Rectangle(125, 33, 38, 63))
    );
    this.attackTexture.push(
      new PIXI.Texture(baseAttackTexture, new PIXI.Rectangle(220, 33, 70, 63))
    );
    this.attackTexture.push(
      new PIXI.Texture(baseAttackTexture, new PIXI.Rectangle(316, 33, 61, 63))
    );

    const baseDeadTexture = PIXI.BaseTexture.from(
      "/public/enemy/Orc_Berserk/Dead.png"
    );
    this.deadTexture = [];
    this.deadTexture.push(
      new PIXI.Texture(baseDeadTexture, new PIXI.Rectangle(7, 35, 40, 61))
    );
    this.deadTexture.push(
      new PIXI.Texture(baseDeadTexture, new PIXI.Rectangle(105, 35, 40, 61))
    );
    this.deadTexture.push(
      new PIXI.Texture(baseDeadTexture, new PIXI.Rectangle(200, 44, 54, 42))
    );
    this.deadTexture.push(
      new PIXI.Texture(baseDeadTexture, new PIXI.Rectangle(297, 67, 70, 29))
    );
  }
}
